package com.jdew.thkj.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jdew.thkj.R;
import com.jdew.thkj.base.BaseActivity;
import com.jdew.thkj.base.BaseFragment;
import com.jdew.thkj.fragment.FragmentFactory;
import com.jdew.thkj.view.NetworkProgressDialog;

import butterknife.Bind;

/**
 * Created by yangzai on 2018/4/20 0020.
 */

public class MyFansActivity extends BaseActivity {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.tab)
    TabLayout tab;
    @Bind(R.id.viewpager)
    ViewPager viewpager;
    public SharedPreferences sp;
    public SharedPreferences.Editor editor;
    public NetworkProgressDialog networkProgressDialog;
    public int type;

    @Override
    public int bindLayout() {
        return R.layout.activity_my_fans;
    }

    @Override
    public void initView(View view) {
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(this);
        title.setText("我的粉丝");
        type=getIntent().getIntExtra("type",2);
        sp = getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        ShortPagerAdapter adapter = new ShortPagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(adapter);
        tab.setupWithViewPager(viewpager);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back: {
                finish();
            }
            break;
            default:
                break;
        }
    }

    private class ShortPagerAdapter extends FragmentPagerAdapter {
        public String[] mTilte=null;

        public ShortPagerAdapter(FragmentManager fm) {
            super(fm);
            if (sp.getString("SpareTwo", "").equals("0")) {
                mTilte = getResources().getStringArray(R.array.tab_long_Title);
            } else {
                mTilte = getResources().getStringArray(R.array.tab_short_Title);
            }
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTilte[position];
        }

        @Override
        public BaseFragment getItem(int position) {
            BaseFragment fragment=null;
            if (sp.getString("SpareTwo", "").equals("0")) {
                fragment = FragmentFactory.createFragment2(position);
            } else {
                fragment = FragmentFactory.createFragment(position);
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return mTilte.length;
        }
    }

    /**
     * 根据手机的分辨率从 dip 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
